package com.js.purs.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.js.purs.domain.PurProdBudget;
import com.js.purs.domain.PurProdPlan;
import com.js.purs.mapper.PurProdBudgetMapper;
import com.js.purs.mapper.PurProdPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.js.purs.mapper.PurProdInfoMapper;
import com.js.purs.domain.PurProdInfo;
import com.js.purs.service.IPurProdInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购商品信息Service业务层处理
 *
 * @author lzq
 * @date 20240617
 */
@Service
public class PurProdInfoServiceImpl implements IPurProdInfoService {
    @Autowired
    private PurProdInfoMapper purProdInfoMapper;

    @Autowired
    private PurProdPlanMapper purProdPlanMapper;

    @Autowired
    private PurProdBudgetMapper purProdBudgetMapper;

    /**
     * 查询采购商品信息
     *
     * @param prodInfoId 采购商品信息主键
     * @return 采购商品信息
     */
    @Override
    public PurProdInfo selectPurProdInfoByProdInfoId(Long prodInfoId) {
        return purProdInfoMapper.selectPurProdInfoByProdInfoId(prodInfoId);
    }

    /**
     * 查询采购商品信息列表
     *
     * @param purProdInfo 采购商品信息
     * @return 采购商品信息
     */
    @Override
    public List<PurProdInfo> selectPurProdInfoList(PurProdInfo purProdInfo) {
        return purProdInfoMapper.selectPurProdInfoList(purProdInfo);
    }

    /**
     * 新增采购商品信息
     *
     * @param purProdInfo 采购商品信息
     * @return 结果
     */
    @Override
    public int insertPurProdInfo(PurProdInfo purProdInfo) {
        return purProdInfoMapper.insertPurProdInfo(purProdInfo);
    }

    /**
     * 修改采购商品信息
     *
     * @param purProdInfo 采购商品信息
     * @return 结果
     */
    @Override
    public int updatePurProdInfo(PurProdInfo purProdInfo) {
        return purProdInfoMapper.updatePurProdInfo(purProdInfo);
    }

    /**
     * 批量删除采购商品信息
     *
     * @param prodInfoIds 需要删除的采购商品信息主键
     * @return 结果
     */
    @Override
    public int deletePurProdInfoByProdInfoIds(Long[] prodInfoIds) {
        return purProdInfoMapper.deletePurProdInfoByProdInfoIds(prodInfoIds);
    }

    /**
     * 删除采购商品信息信息
     *
     * @param prodInfoId 采购商品信息主键
     * @return 结果
     */
    @Override
    public int deletePurProdInfoByProdInfoId(Long prodInfoId) {
        return purProdInfoMapper.deletePurProdInfoByProdInfoId(prodInfoId);
    }

    @Override
    public int insertPlanInfoBatch(List<PurProdInfo> purProdInfos) {
        return purProdInfoMapper.insertPlanInfoBatch(purProdInfos);
    }

    @Override
    public List<PurProdInfo> selectPurProdInfoByBgId(String bgId) {
        return purProdInfoMapper.selectPurProdInfoByBgId(bgId);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateOrInsertPlanInfo(List<PurProdInfo> purProdInfos) {
        Integer code = 1;
        for (PurProdInfo info : purProdInfos) {

            if (info.getProdInfoId() != null) {
                code = purProdInfoMapper.updatePurProdInfo(info);
            } else {
                code = purProdInfoMapper.insertPurProdInfo(info);
            }
        }
        return 1;
    }

    @Override
    public int deletePlanInfos(List<PurProdInfo> purProdInfos) {
        Integer code = 1;
        List<PurProdInfo> infos = purProdInfoMapper.selectPurProdInfoList(new PurProdInfo());

        for (PurProdInfo info : purProdInfos) {
            if (info.getProdInfoId() != null) {
                code = purProdInfoMapper.deletePurProdInfoByProdInfoId(info.getProdInfoId());
            }

        }
        return 1;
    }

    @Override
    public PurProdInfo selectPurProdInfoNewId() {
        return purProdInfoMapper.selectPurProdInfoNewId();
    }

    @Override
    public List<PurProdInfo> getPlanInfoListByBudgets(String planCode) {
        List<PurProdInfo> list =new ArrayList<>();
        List<PurProdBudget> budgets = purProdBudgetMapper.selectPurProdBudgetByPlanCode(planCode);
        for (PurProdBudget budget : budgets) {
            List<PurProdInfo> purProdInfos = purProdInfoMapper.selectPurProdInfoByBgId(budget.getBgId());
            for (PurProdInfo info : purProdInfos) {
                list.add(info);
            }
        }
        return list;
    }


}
